<?php
include_once(realpath(dirname(__FILE__)) . "/../../../classes/PluginsClassiques.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Message.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Messagedesc.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Variable.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Mail.class.php");



class Contact extends PluginsClassiques{

	var $id;
	var $nom;
	var $prenom;
	var $email;
	var $tel;
	var $sujet;
	var $message;
	var $adresse;
	var $cpostal;
	var $ville;
	var $date;
	var $lu;
	
	var $table = "contact";
	var $bddvars = array("id","nom","prenom","email","tel","sujet","message", "adresse", "cpostal", "ville", "date","lu");
	
	
	function Contact(){
		$this->PluginsClassiques();
	}

	function init(){
		$variable = new Variable();
		if(!$variable->charger("contact")){
			$emailcontact = new variable();
			$emailcontact->charger("emailcontact");
			
			$variable->nom = "contact";
			$variable->valeur = $emailcontact->valeur;
			$variable->add();
		}
	
		$message = new Message();
		if(!$message->charger("contact")){
			$message->nom = "contact";
						
			$messagedesc = new Messagedesc();
			$messagedesc->message = $message->add();
			$messagedesc->lang="1";
			$messagedesc->intitule = "contact";
			$messagedesc->titre = "__SUJET__";
			$messagedesc->description = "Bonjour,<br /> Un nouveau mail de contact vient de vous être adressé : <br />nom : __NOM__<br />prénom : __PRENOM__<br />email : __EMAIL__<br />message : __MESSAGE__";
			$messagedesc->descriptiontext = str_replace("<br />","\r\n",$messagedesc->description);
			$messagedesc->add();
		}
		
		$cnx = new Cnx();
		$query = "CREATE TABLE `contact` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`nom` VARCHAR( 255 ) NOT NULL ,
`prenom` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`tel` VARCHAR( 30 ) NOT NULL ,
`sujet` VARCHAR( 255 ) NOT NULL ,
`message` TEXT NOT NULL ,
`adresse` VARCHAR( 255 ) NOT NULL ,
`cpostal` VARCHAR( 255 ) NOT NULL ,
`ville` VARCHAR( 255 ) NOT NULL ,
`date` DATETIME NOT NULL ,
`lu` INT NOT NULL
)";
		$resul = mysql_query($query,$cnx->link);
	}
	
	function charger($id){
		return $this->getVars("select * from $this->table where id=\"$id\"");
	}

	function varfond(){
		global $res, $obligenom, $obligeprenom, $obligetel, $obligesujet, $obligemessage, $obligeadresse, $obligecpostal, $obligeville;

		$fondvars = array(
			 'obligenom',
			 'obligeprenom',
			 'obligetel',
			 'obligesujet',
			 'obligemessage',
			 'obligeadresse',
			 'obligecpostal',
			 'obligeville'
			
		);

		if (preg_match_all("/#PARAM_FOND_([a-zA-Z0-9_]+)[\s]*=[\s]*([0-9]*)[\s]*/", $res, $matches, PREG_SET_ORDER)) {
			foreach($matches as $match) {
				$var = strtolower($match[1]);

				if (in_array($var, $fondvars)) {
					$$var = $match[2];

					$res = str_replace($match[0], '', $res);
				}
			}
		}
				
	
	}
		
	function action(){
		global $action;
		
		switch($action){
			case "contact":
				$this->crea_contact();
				break;
		}
		
		if(isset($_GET["errcontact"]) && $_GET["errcontact"] == 1){
			$this->formerreur();
		}
	}
	
	function demmarrage(){
		if(!isset($_SESSION["contact"])){
			$_SESSION["contact"] = new Contact();
		}
	}
	
	function formerreur(){
		global $res;
		if( $_SESSION['contact']->nom == "" ) $res = preg_replace("/\#NOM\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#NOM\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->prenom == "" ) $res = preg_replace("/\#PRENOM\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#PRENOM\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->email == "" ) $res = preg_replace("/\#EMAIL\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#EMAIL\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->tel == "" ) $res = preg_replace("/\#TEL\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#TEL\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->sujet == "" ) $res = preg_replace("/\#SUJET\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#SUJET\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->message == "" ) $res = preg_replace("/\#MESSAGE\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#MESSAGE\[[^]]*\]/", "", $res);
	 	
	 	if( $_SESSION['contact']->adresse == "" ) $res = preg_replace("/\#ADRESSE\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#ADRESSE\[[^]]*\]/", "", $res);
	
	 	if( $_SESSION['contact']->cpostal == "" ) $res = preg_replace("/\#CPOSTAL\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#CPOSTAL\[[^]]*\]/", "", $res);
	
	 	if( $_SESSION['contact']->ville == "" ) $res = preg_replace("/\#VILLE\[([^]]*)\]/", "\\1", $res);
	 	else $res = preg_replace("/\#VILLE\[[^]]*\]/", "", $res);
				
	 	$res = str_replace("#NOM",$_SESSION["contact"]->nom,$res);
	 	$res = str_replace("#PRENOM",$_SESSION["contact"]->prenom,$res);
	 	$res = str_replace("#EMAIL",$_SESSION["contact"]->email,$res);
	 	$res = str_replace("#TEL",$_SESSION["contact"]->tel,$res);
	 	$res = str_replace("#SUJET",$_SESSION["contact"]->sujet,$res);
	 	$res = str_replace("#MESSAGE",$_SESSION["contact"]->message,$res);
	 	$res = str_replace("#ADRESSE",$_SESSION["contact"]->adresse,$res);
	 	$res = str_replace("#CPOSTAL",$_SESSION["contact"]->cpostal,$res);
	 	$res = str_replace("#VILLE",$_SESSION["contact"]->ville,$res);
	}
	
	function crea_contact(){
		global $obligenom, $obligeprenom, $obligetel, $obligesujet, $obligemessage, $obligeadresse, $obligecpostal, $obligeville;
		
		$urlsite = new Variable("urlsite");
		
		$contact = new Contact();
		$contact->nom = strip_tags($_POST["nom"]);
		$contact->prenom = strip_tags($_POST["prenom"]);
		if( preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z.]+$/",$_POST["email"])) $contact->email = strip_tags($_POST["email"]);
		$contact->tel = strip_tags($_POST["tel"]);
		$contact->sujet = strip_tags($_POST["sujet"]);
		$contact->message = strip_tags($_POST["message"]);
		$contact->adresse = strip_tags($_POST["adresse"]);
		$contact->cpostal = strip_tags($_POST["cpostal"]);
		$contact->ville = strip_tags($_POST["ville"]);
		
		$obligeok = 1;
		
		if($obligenom && $contact->nom == "") $obligeok = 0;
		if($obligeprenom && $contact->prenom == "") $obligeok = 0;
		if($obligetel && $contact->tel == "") $obligeok  =0;
		if($obligesujet && $contact->sujet == "") $obligeok = 0;
		if($obligemessage && $contact->message == "") $obligeok = 0;
		if($obligeadresse && $contact->adresse == "") $obligeok = 0;
		if($obligecpostal && $contact->cpostal == "") $obligeok = 0;
		if($obligeville && $contact->ville == "") $obligeok = 0;
		if($contact->email == "") $obligeok = 0;
		
		if(!$obligeok){
			$_SESSION["contact"] = $contact;
			redirige($urlsite->valeur . "?fond=contacterr&errcontact=1");
		}
		else{
			$contact->date = date("Y-m-d H:i:s");
			$contact->lu = 0;
			$contact->add();
			$this->envoimessage($contact);
		}
	}
	
	function envoimessage($contact){
		$message = new Message();
		$message->charger("contact");
		
		$messagedesc = new Messagedesc();
		$messagedesc->charger($message->id);
		
		$email = new Variable();
		$email->charger("contact");
		
		$nomsite = new Variable();
		$nomsite->charger("nomsite");
		
		$mail = new Mail();
		$mail->IsMail();
		$mail->FromName = utf8_decode($contact->nom);
		$mail->From = $contact->email;
		$mail->Subject = utf8_decode($this->substitmail($messagedesc->titre,$contact));
		$mail->MsgHTML(utf8_decode($this->substitmail($messagedesc->description,$contact)));
		$mail->AltBody = utf8_decode($this->substitmail($messagedesc->descriptiontext,$contact));
		$mail->AddAddress($email->valeur,utf8_decode($nomsite->valeur));
		$mail->send();
	
	}
	
	function substitmail($corps,$contact){
		$corps = str_replace("__SUJET__",$contact->sujet,$corps);
		$corps = str_replace("__NOM__",$contact->nom,$corps);
		$corps = str_replace("__PRENOM__",$contact->prenom,$corps);
		$corps = str_replace("__TEL__",$contact->tel,$corps);
		$corps = str_replace("__EMAIL__",$contact->email,$corps);
		$corps = str_replace("__MESSAGE__",$contact->message,$corps);
		$corps = str_replace("__ADRESSE__",$contact->adresse,$corps);
		$corps = str_replace("__CPOSTAL__",$contact->cpostal,$corps);
		$corps = str_replace("__VILLE__",$contact->ville,$corps);
		
		return $corps;
	}
}


?>